import config from './config.js';
import axios from "axios";
// import qs from "qs";

import router from "@/router"
import util from "@/utils/util"

import {
	ElLoading,
	ElMessage
} from 'element-plus'

import {
	ref,
	computed,
} from 'vue';

const loading = ref();
const fullscreen = ref(false);

//	post请求
export const request = function(params) {

	return new Promise((resolve, reject) => {

		//	请求地址
		var url = config.host + (params.url ? params.url : '');

		if (params.load) {
			if (!fullscreen.value) {
				// fullscreen.value = true;

				// loading.value = ElLoading.service({
				// 	lock: true,
				// 	text: 'Loading',
				// 	background: 'rgba(0, 0, 0, 0.7)',
				// });
			}
		}

		//	定义数据
		params.data = params.data || {};

		// 接口访问类型
		const method = params.method || "POST";

		// application/json 请求数组配置
		let headers = params.headers || {
			'Content-Type': ['get', "GET"].includes(method) ?
				'application/x-www-form-urlencoded;charset=UTF-8' : "application/json;charset=UTF-8",
		}

		const token = localStorage.getItem("token") || '';
		headers.Authorization = `Bearer ${token}`;

		// 接口參數
		let axiosData = {
			data: params.data,
			params: params.data,
		}

		if (['POST', 'post', 'PUT'].includes(method)) {
			delete axiosData.params
		} else {
			delete axiosData.data
		}

		let axios_data = {
			headers,
			url,
			method,
			timeout: 99999,
			...axiosData,

			dataType: "json",
		};

		if (params['responseType']) {
			axios_data['responseType'] = params['responseType']
		}
		axios(axios_data).then(res => {
			// console.log( params.url);
			fullscreen.value = false;
			if (params.load && loading.value) loading.value.close();

			// 下载
			// if (['/business/form/export', '/business/form/exportPdf'].includes(params.url)) {
			if (params.responseType && params.responseType == "blob") {
				resolve(res)
				return;
			}

			// 请求异常
			if (res.data.code != 200) {

				// 弹窗提示
				if (params.alert == true) {
					ElMessage.warning(res.data.msg)
				}

				// 未登录
				if (res.data.code == 401) {
					localStorage.setItem("token", '');
					util.to('/login')
					return;
				}

				// resolve('');
				return '';
			}

			resolve(res.data || {})

		}).catch(err => {
			fullscreen.value = false;
			if (params.load && loading.value) loading.value.close();

			if (err && err.response && err.response.status == 401) {
				return
			}

			reject(err)
		})
	})
}


// post请求
export const upload = function(params) {

	return new Promise((resolve, reject) => {

		//	请求地址
		var url = config.host + (params.url || config.upload_file_api);

		if (params.load) {
			loading = ElLoading.service({
				lock: true,
				text: 'Loading',
				background: 'rgba(0, 0, 0, 0.7)',
			});
		}

		let headers = params.header || {
			'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
		}
		const token = localStorage.getItem("token") || '';
		headers.Authorization = `Bearer ${token}`;

		//	初始化表单数据
		var forms = new FormData();
		//	将文件塞入表单里去
		forms.append('file', params.file)
		// .target.files[0]

		// 发起网络请求
		axios({
				headers,
				url,
				method: params.method || "POST",
				data: forms,
			}).then((res) => {

				if (res.data.code != 200) {
					ElMessage.warning(res.data.msg)
					return '';
				}

				fullscreen.value = false;
				if (params.load) loading.value.close();
				resolve(res.data)
			})
			.catch((err) => {

				fullscreen.value = false;
				if (params.load) loading.value.close();
				reject(err)
			})
	})

}